<template>
  <div class="checkout-address">
    <div class="text">
      <div class="none" v-if="!showAddress">您需要先添加收货地址才可提交订单。</div>
      <ul v-if="showAddress">
        <li><span>收<i/>货<i/>人：</span>{{showAddress.receiver}}</li>
        <li><span>联系方式：</span>{{showAddress.contact.replace(showAddress.contact.substr(3, 4), '****')}}</li>
        <li><span>收货地址：</span>{{showAddress.fullLocation}}{{showAddress.address}}</li>
      </ul>
      <a href="javascript:;" v-if="showAddress" @click="openAddressEdit(showAddress)">修改地址</a>
    </div>
    <div class="action">
      <el-button type="primary" @click="openDialog()">切换地址</el-button>
      <el-button type="primary" @click="openAddressEdit({})">添加地址</el-button>
    </div>
  </div>
  <el-dialog
    v-model="dialogVisible"
    title="切换收货地址"
    width="30%"
  >
    <!-- <span>This is a message</span> -->
    <!-- 对话框内容 切换收货地址-->
    <div class="text item" v-for="item in userAddresses" :key="item.id" :class="{active: selectedAddress===item}" @click="selectedAddress = item">
      <ul>
        <li><span>收<i/>货<i/>人：</span>{{item.receiver}}</li>
        <li><span>联系方式：</span>{{item.contact.replace(item.contact.substr(3, 4), '****')}}</li>
        <li><span>收货地址：</span>{{item.fullLocation}}{{item.address}}</li>
      </ul>
    </div>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" @click="confirmAddress()"
          >确认</el-button
        >
      </span>
    </template>
  </el-dialog>
  <!-- 编辑修改组件 -->
  <AddressEdit ref="addressEditCom" @on-success="successHandler"></AddressEdit>
</template>
<script>
import { ref } from 'vue-demi'
import AddressEdit from './address-edit.vue'
export default {
  name: 'CheckoutAddress',
  components: {
    AddressEdit
  },
  props: {
    userAddresses: {
      type: Array,
      default: () => {}
    }
  },
  emits: ['change'],
  setup (props, { emit }) {
    const showAddress = ref(null)
    if (props.userAddresses.length) {
      const defaultAddress = props.userAddresses.find(list => list.isDefault === 0)
      if (defaultAddress) {
        showAddress.value = defaultAddress
      } else {
        if (props.userAddresses.length) {
          // eslint-disable-next-line vue/no-setup-props-destructure
          showAddress.value = props.userAddresses[0]
          // console.log(showAddress.contact.substr(3, 4))
        }
      }
    }
    const dialogVisible = ref(false)
    // 默认通知一个地址ID给父
    emit('change', showAddress.value && showAddress.value.id)
    const selectedAddress = ref(null)
    // 打开对话框
    const openDialog = () => {
      // 数据开始时置为默认的地址
      selectedAddress.value = showAddress.value
      dialogVisible.value = true
    }
    // 点击确认切换地址
    const confirmAddress = () => {
      showAddress.value = selectedAddress.value
      // 把选中地址id通知结算组件
      emit('change', selectedAddress.value && selectedAddress.value.id)
      dialogVisible.value = false
    }
    // 打开添加地址组件
    const addressEditCom = ref(null)
    // 根据传入的对象不同 表示添加或修改
    const openAddressEdit = (showAdd) => {
      // console.log(showAdd)
      addressEditCom.value.open(showAdd)
    }
    // 编辑或修改文件
    const successHandler = (form) => {
      // 修改
      const editAddress = props.userAddresses.find(item => item.id === form.id)
      // console.log(index)
      // 有 是修改
      if (editAddress) {
        // const jsonStr = JSON.stringify(form)
        // eslint-disable-next-line vue/no-mutating-props
        // props.userAddresses[index] = JSON.parse(jsonStr)
        for (const key in editAddress) {
          if (Object.hasOwnProperty.call(editAddress, key)) {
            editAddress[key] = form[key]
          }
        }
      } else {
        // 如果是添加  就在userAddresses中添加一项地址
        // 在修改form时候 数组中·的数据也会更新，因为是同一个引用地址
        // 在打开对话框之前需要清空之前的输入信息
        // 解决:克隆form
        const jsonStr = JSON.stringify(form)
        // eslint-disable-next-line vue/no-mutating-props
        props.userAddresses.unshift(JSON.parse(jsonStr))
        // console.log(props.userAddresses)
      }
    }
    return { showAddress, dialogVisible, selectedAddress, confirmAddress, openDialog, openAddressEdit, addressEditCom, successHandler }
  }
}
</script>
<style scoped lang="less">
@import '../../../../assets/styles/variable.less';
.checkout-address {
  border: 1px solid #f5f5f5;
  display: flex;
  align-items: center;
  .text {
    flex: 1;
    min-height: 90px;
    display: flex;
    align-items: center;
    .none {
      line-height: 90px;
      color: #999;
      text-align: center;
      width: 100%;
    }
    > ul {
      flex: 1;
      padding: 20px;
      li {
        line-height: 30px;
        span {
          color: #999;
          margin-right: 5px;
          > i {
            width: 0.5em;
            display: inline-block;
          }
        }
      }
    }
    > a {
      color: @c;
      width: 160px;
      text-align: center;
      height: 90px;
      line-height: 90px;
      border-right: 1px solid #f5f5f5;
    }
  }
  .action {
    width: 420px;
    text-align: center;
    .btn {
      width: 140px;
      height: 46px;
      line-height: 44px;
      font-size: 14px;
      &:first-child {
        margin-right: 10px;
      }
    }
  }
}
.text {
    flex: 1;
    min-height: 90px;
    display: flex;
    align-items: center;
    &.item {
      border: 1px solid #f5f5f5;
      margin-bottom: 10px;
      cursor: pointer;
      &.active,&:hover {
        border-color: @c;
        background: lighten(@c,50%);
      }
      > ul {
        padding: 10px;
        font-size: 14px;
        line-height: 30px;
      }
    }
  }
</style>
